package com.zj.executor;

import com.zj.executor.result.ResultHandler;
import com.zj.executor.statement.PreparedStatementHandler;
import com.zj.executor.statement.StatementHandler;
import com.zj.mapping.BoundSql;
import com.zj.mapping.Configration;
import com.zj.mapping.MappedStatement;

import java.util.List;

public class SimpleExcecuter implements Excecuter {

    private Configration configration;

    public SimpleExcecuter(Configration configration) {
        this.configration = configration;
    }

    @Override
    public <T> List<T> query(MappedStatement mappedStatement, ResultHandler resultHandler, Object... params) throws Exception {

        BoundSql boundSql=mappedStatement.getBoundSql();

        StatementHandler statementHandler = new PreparedStatementHandler(configration);

        return statementHandler.query(mappedStatement,boundSql,resultHandler,params);

    }

    @Override
    public Integer insert(MappedStatement mappedStatement, Object... params) throws Exception {
        return update(mappedStatement,params);
    }

    @Override
    public Integer update(MappedStatement mappedStatement, Object... params) throws Exception {

        BoundSql boundSql=mappedStatement.getBoundSql();

        StatementHandler statementHandler = new PreparedStatementHandler(configration);

        return statementHandler.update(mappedStatement,boundSql,params);
    }

    @Override
    public Integer delete(MappedStatement mappedStatement, Object... params) throws Exception {
        return update(mappedStatement,params);
    }


}
